﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System.Collections.Generic;
using System.Linq;

namespace OestsServer.Handlers.OESPHandlers
{
    public class PaperTableHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.PaperTable;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);

            // List<Paper> list = PaperDAL.GetData(client.User.DBid);//TODO:是否只获取当前登录教员用户创建的试卷集？
            List<Paper> list = PaperDAL.GetData();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }
    }
    public class AddPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.AddPaper;
            }
        }
        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            Paper paper = JsonMapper.ToObject<Paper>(packet.DataStr);
            int paperId = PaperDAL.AddPaper(paper, client.User.DBid);
            paper.Id = paperId;
            //client.Send(PacketKey.AddPaper, data);//反馈请求数据
            ResponsePacket.DataObject = paper;
            client.Send(ResponsePacket);
        }
    }

    public class DelPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.DeletePaper;
            }
        }


        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<int> data = JsonMapper.ToObject<List<int>>(packet.DataStr);
            List<int> list = data.Where(i => PaperDAL.DelPaper(i)).ToList();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }

    }

    public class ModifyPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.ModifyPaper;
            }
        }
        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            Paper paper = JsonMapper.ToObject<Paper>(packet.DataStr);
            if (PaperDAL.ModifyPaper(paper))
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                ResponsePacket.DataObject = paper;
            }
            else
            {
                ResponsePacket.DataObject = null;
                ResponsePacket.ReturnCode = ReturnCode.Fail;
            }
            client.Send(ResponsePacket);
        }
    }
    /// <summary>
    /// 获取固定理论卷中的试题
    /// </summary>
    public class GetSubjectInPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.TheoryPaperDataTable;

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<TheoryPaperData> list = PaperDAL.GetTheoryPaperDatas();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }

    }

    /// <summary>
    /// 获取随机理论卷中的试题数量及分值
    /// </summary>
    public class GetSubjectInRandomPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.RandomTheoryPaperDataTable;

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<RandomTheoryPaperData> list = PaperDAL.GetRandomTheoryPaperDatas();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }

    }

    /// <summary>
    /// 管理随机理论试卷中的试题
    /// </summary>
    public class ManageSubjectInRandomPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.ManageSubjectInRandomPaper;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            RandomTheoryPaperData data = JsonMapper.ToObject<RandomTheoryPaperData>(resquestPacket.DataStr);

            PaperDAL.ClearSubjectsInPaper(data.PaperId);
            PaperDAL.ManageSubjectInRandomPaper(data);

            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);
        }
    }

    /// <summary>
    /// 管理固定理论试卷中的试题
    /// </summary>
    public class ManageSubjectInPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.ManageSubjectInPaper;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            TheoryPaperData data = JsonMapper.ToObject<TheoryPaperData>(resquestPacket.DataStr);
            //先将试卷中的试题ID保存下来
            List<int> oldSubjectIds = PaperDAL.GetSujectIds(data.PaperId);

            PaperDAL.ClearSubjectsInPaper(data.PaperId);
            data.SubjectData.ForEach(m => { PaperDAL.ManageSubjectInPaper(data.PaperId, m.Value1, m.Value2); });

            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);

            //更新试题的使用次数

            //新的试题ID
            List<int> newIds = data.SubjectData.Select(x => { return x.Value1; }).ToList();
            List<int> chajiList = newIds.Except(oldSubjectIds).ToList();//新添加的试题

            List<Int2> reList = new List<Int2>();
            foreach (var item in chajiList)
            {
                int count = SubjectDAL.UpdateUseCount(item);
                reList.Add(new Int2 { Int0 = item, Int1 = count });
            }
            Response r = new Response { ReturnCode = ReturnCode.Success, ResponseCode = PacketKey.UpdateSubjectUseCount, DataObject = reList };

            OESPClientChannel.BrocastToTeacher.Dispatch(r);

        }
    }

    public class ManageTaskInPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get

            {
                return PacketKey.ManageTaskInPaper;
            }
        }

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            OperatePaperData data = JsonMapper.ToObject<OperatePaperData>(packet.DataStr);

            PaperDAL.ClearTasksInPaper(data.PaperId);
            data.TaskData.ForEach(m => { PaperDAL.ManageTaskInPaper(data.PaperId, m.Value1, m.Value2); });
            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);
        }
    }

    public class ManageTaskInRandomPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get

            {
                return PacketKey.ManageTaskInRandomPaper;
            }
        }

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            RandomOperatePaperData data = JsonMapper.ToObject<RandomOperatePaperData>(packet.DataStr);

            PaperDAL.ClearTasksInPaper(data.PaperId);
            PaperDAL.ManageTaskInRandomPaper(data);
            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);
        }
    }


    public class GetTaskInPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.OperatePaperDataTable;

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<OperatePaperData> list = PaperDAL.GetOperatePaperDatas();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }
    }

    public class GetTaskInRandomPaperHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.RandomOperatePaperDataTable;

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<RandomOperatePaperData> list = PaperDAL.GetRandomOperatePaperDatas();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }
    }
}
